package thread;

/**
 * @author ten_fei
 * @create 2021-02-16 20:15
 */

import java.util.concurrent.locks.ReentrantLock;

/**
 * 线程同步两种方法
 * 一、同步代码块
 * 二、同步方法
 */
public class ThreadSynchronization {
    public static void main(String[] args) {

//        ThreadAS1 as1 = new ThreadAS1();
//        ThreadAS1 as2 = new ThreadAS1();
//        ThreadAS1 as3 = new ThreadAS1();
//
//        as1.setName("窗口一");
//        as2.setName("窗口二");
//        as3.setName("窗口三");
//
//        as1.start();
//        as2.start();
//        as3.start();

//        ThreadAS2 ts2 = new ThreadAS2();
//        Thread s1 = new Thread(ts2);
//        Thread s2 = new Thread(ts2);
//        Thread s3 = new Thread(ts2);
//
//        s1.setName("窗口一");
//        s2.setName("窗口二");
//        s3.setName("窗口三");
//
//        s1.start();
//        s2.start();
//        s3.start();


//        ThreadAS3 as1 = new ThreadAS3();
//        ThreadAS3 as2 = new ThreadAS3();
//        ThreadAS3 as3 = new ThreadAS3();
//
//        as1.setName("窗口一");
//        as2.setName("窗口二");
//        as3.setName("窗口三");
//
//        as1.start();
//        as2.start();
//        as3.start();


//        ThreadAS4 ts4 = new ThreadAS4();
//        Thread s1 = new Thread(ts4);
//        Thread s2 = new Thread(ts4);
//        Thread s3 = new Thread(ts4);
//
//        s1.setName("窗口一");
//        s2.setName("窗口二");
//        s3.setName("窗口三");
//
//        s1.start();
//        s2.start();
//        s3.start();

        ThreadAS6 ts6 = new ThreadAS6();
        Thread s1 = new Thread(ts6);
        Thread s2 = new Thread(ts6);
        Thread s3 = new Thread(ts6);

        s1.setName("窗口一");
        s2.setName("窗口二");
        s3.setName("窗口三");

        s1.start();
        s2.start();
        s3.start();
    }
}


//同步代码块继承Thread类
class ThreadAS1 extends Thread {
    private static int ticket = 100;
    private static Object obj2 = new Object();

    public void run() {
        while (true) {
//            synchronized (obj2) {
            synchronized (ThreadAS1.class) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(getName() + "卖的票，票号为：" + ticket);
                    ticket--;
                } else {
                    break;
                }
            }
        }
    }
}


//同步代码块实现Runable接口
class ThreadAS2 implements Runnable {
    private int ticket = 100;
    Object obj1 = new Object();

    public void run() {
        while (true) {
            synchronized (obj1) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "卖票，票号为：" + ticket);
                    ticket--;
                } else {
                    break;
                }
            }
        }
    }
}

//同步方法继承Thread类
class ThreadAS3 extends Thread {
    private static int ticket = 100;

    public void run() {
        while (true) {
            show();
        }
    }

    private static synchronized void show() {
        if (ticket > 0) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "卖票，票号为：" + ticket);
            ticket--;
        }
    }
}


//同步方法实现Runable接口
class ThreadAS4 implements Runnable {
    private static int ticket = 100;

    public void run() {
        while (true) {
            show();
        }
    }

    private synchronized void show() {
        if (ticket > 0) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "卖票，票号为：" + ticket);
            ticket--;
        }
    }
}


//同步锁继承Thread类
class ThreadAS5 extends Thread {
    private static int ticket = 100;

    public void run() {
        while (true) {
            if (ticket > 0) {
                System.out.println(getName() + "卖票，票号为：" + ticket);
                ticket--;
            } else {
                break;
            }
        }
    }
}


//同步锁实现Runable方法
class ThreadAS6 implements Runnable {
    private static int ticket = 100;
    private ReentrantLock lock = new ReentrantLock();

    public void run() {
        while (true) {
            try {
                lock.lock();
                if (ticket > 0) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "卖票，票号为：" + ticket);
                    ticket--;
                } else {
                    break;
                }
            } finally {
                lock.unlock();
            }
        }
    }
}
















